# -*- encoding: utf-8 -*-

from google.appengine.api import users

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from users import login_required
from sport_metrics import get_context
from sport_metrics.models import CardiacFrequency
from user_metrics.models import UserMetricsView, Weight, UserParameters

@login_required
def health_view(request):
    """Show a summary of the most usefull user informations"""
    from metrics.forms import DateForm
    user = users.get_current_user()
    health_metrics_view = UserMetricsView.get_or_insert('health',
                                                        name='health')
    date_form = DateForm(request.POST,
                         prefix='date')
    if request.method == 'POST' and date_form.is_valid():
        date = date_form.cleaned_data['date']
        weight_result, weight_form = Weight.add_data(user,
                                                     date,
                                                     health_metrics_view,
                                                     request.POST)
        cardiac_frequency_result, cardiac_frequency_form = CardiacFrequency.add_data(user,
                                                                                     date,
                                                                                     health_metrics_view,
                                                                                     request.POST)
        if weight_result and cardiac_frequency_result:
            return HttpResponseRedirect(request.path)
    else:
        date_form = DateForm(prefix='date')
        weight_form = Weight.get_form()
        cardiac_frequency_form = CardiacFrequency.get_form()
        
    weight_chart_url = Weight.get_chart_url(user,
                                            health_metrics_view)
    cardiac_frequency_chart_url = CardiacFrequency.get_chart_url(user,
                                                                 health_metrics_view)
    return render_to_response('user_metrics/health_view.html', 
                              {'date_form':date_form,
                               'weight_form':weight_form,
                               'weight_chart_url':weight_chart_url,
                               'cardiac_frequency_form':cardiac_frequency_form,
                               'cardiac_frequency_chart_url':cardiac_frequency_chart_url}, 
                              get_context())



@login_required
def parameters_view(request):
    user = users.get_current_user()
    userparameters = UserParameters.gql('WHERE user = :1', user).get()
    if not userparameters:
        userparameters = UserParameters(user=user)
    if request.method == 'POST':
        valid, form = userparameters.set_data(request.POST)
        if valid:
            return HttpResponseRedirect('/')
    else:
        form = userparameters.get_form()
    return render_to_response('user_metrics/parameters_view.html', 
                              {'form': form},
                              get_context())
